Convert.ToInt32 方法 (System)

您所在的位置:网站首页 convert reverse 区别 Convert.ToInt32 方法 (System)

Convert.ToInt32 方法 (System)

2023-08-19 04:15| 来源: 网络整理| 查看: 265

将指定基数的数字的字符串表示形式转换为等效的 32 位有符号整数。

public: static int ToInt32(System::String ^ value, int fromBase); public static int ToInt32 (string value, int fromBase); public static int ToInt32 (string? value, int fromBase); static member ToInt32 : string * int -> int Public Shared Function ToInt32 (value As String, fromBase As Integer) As Integer 参数 value String

包含要转换的数字的字符串。

fromBase Int32

value 中数字的基数,它必须是 2、8、10 或 16。

返回 Int32

一个与 value 中数字等效的 32 位带符号整数,如果 value 为 null,则为 0(零)。

例外 ArgumentException

fromBase 不是 2、8、10 或 16。

value(表示基数不为 10 的符号数字)的前缀为负号。

ArgumentOutOfRangeException

value 上声明的默认值为 Empty。

FormatException

value 包含一个字符,该字符不是由 fromBase 指定的基数中的有效数字。 如果 value 中的第一个字符无效,则该异常消息指示没有要转换的数字;否则,该消息指示 value 包含无效的尾随字符。

OverflowException

value(表示基数不为 10 的符号数字)的前缀为负号。

value 表示小于 Int32.MinValue 或大于 Int32.MaxValue 的数字。

注解

如果 fromBase 为 16,则可以在 参数指定的 value 数字前面加上“0x”或“0X”。

由于非基数 10 数值表示形式不支持负号,因此 该方法 ToInt32(String, Int32) 假定负数使用二的补数表示形式。 换句话说, 方法始终将整数 (位 31) 的最高位二进制位解释为其符号位。 因此,可以编写代码,在该代码中,将数据类型范围 Int32 外的非基数 10 转换为一个 Int32 值,而方法不会引发异常。 以下示例按 1 递增 Int32.MaxValue ,将生成的数字转换为其十六进制字符串表示形式,然后调用 ToInt32(String, Int32) 方法。 方法不引发异常,而是显示消息“0x80000000转换为 -2147483648。

// Create a hexadecimal value out of range of the integer type. String^ value1 = Convert::ToString((static_cast(int::MaxValue)) + 1, 16); // Convert it back to a number. try { int number = Convert::ToInt32(value1, 16); Console::WriteLine("0x{0} converts to {1}.", value1, number); } catch (OverflowException ^e) { Console::WriteLine("Unable to convert '0x{0}' to an integer.", value1); } // The example displays the following output: // 0x80000000 converts to -2147483648. // Create a hexadecimal value out of range of the Integer type. string value = Convert.ToString((long) int.MaxValue + 1, 16); // Convert it back to a number. try { int number = Convert.ToInt32(value, 16); Console.WriteLine("0x{0} converts to {1}.", value, number.ToString()); } catch (OverflowException) { Console.WriteLine("Unable to convert '0x{0}' to an integer.", value); } // Create a hexadecimal value out of range of the Integer type. let value = Convert.ToString(int64 Int32.MaxValue + 1L, 16) // Convert it back to a number. try let number = Convert.ToInt32(value, 16) printfn $"0x{value} converts to {number}." with :? OverflowException -> printfn $"Unable to convert '0x{value}' to an integer." ' Create a hexadecimal value out of range of the Integer type. Dim value As String = Convert.ToString(CLng(Integer.MaxValue) + 1, 16) ' Convert it back to a number. Try Dim number As Integer = Convert.ToInt32(value, 16) Console.WriteLine("0x{0} converts to {1}.", value, number) Catch e As OverflowException Console.WriteLine("Unable to convert '0x{0}' to an integer.", value) End Try

执行二进制运算或数值转换时,开发人员始终负责验证方法是否使用适当的数值表示形式来解释特定值。 如以下示例所示,可以先检索数值的符号,然后再将其转换为其十六进制字符串表示形式,确保方法适当处理溢出。 如果原始值为正,但转换回整数会产生负值,则引发异常。

__int64 sourceNumber2 = (static_cast(int::MaxValue)) + 1; bool isNegative = Math::Sign(sourceNumber2) == -1; String^ value2 = Convert::ToString(sourceNumber2, 16); int targetNumber; try { targetNumber = Convert::ToInt32(value2, 16); if (!(isNegative) & (targetNumber & 0x80000000) != 0) throw gcnew OverflowException(); else Console::WriteLine("0x{0} converts to {1}.", value2, targetNumber); } catch (OverflowException ^e) { Console::WriteLine("Unable to convert '0x{0}' to an integer.", value2); } // The example displays the following output: // Unable to convert '0x80000000' to an integer. // Create a hexadecimal value out of range of the Integer type. long sourceNumber = (long) int.MaxValue + 1; bool isNegative = Math.Sign(sourceNumber) == -1; string value = Convert.ToString(sourceNumber, 16); int targetNumber; try { targetNumber = Convert.ToInt32(value, 16); if (!(isNegative) & (targetNumber & 0x80000000) != 0) throw new OverflowException(); else Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); } catch (OverflowException) { Console.WriteLine("Unable to convert '0x{0}' to an integer.", value); } // Displays the following to the console: // Unable to convert '0x80000000' to an integer. // Create a hexadecimal value out of range of the Integer type. let sourceNumber = int64 Int32.MaxValue + 1L let isNegative = sign sourceNumber = -1 let value = Convert.ToString(sourceNumber, 16) try let targetNumber = Convert.ToInt32(value, 16) if not isNegative && targetNumber &&& 0x80000000 0 then raise (OverflowException()) else printfn $"0x{value} converts to {targetNumber}." with :? OverflowException -> printfn $"Unable to convert '0x{value}' to an integer." // Displays the following to the console: // Unable to convert '0x80000000' to an integer. ' Create a hexadecimal value out of range of the Integer type. Dim sourceNumber As Long = CLng(Integer.MaxValue) + 1 Dim isNegative As Boolean = (Math.Sign(sourceNumber) = -1) Dim value As String = Convert.ToString(sourceNumber, 16) Dim targetNumber As Integer Try targetNumber = Convert.ToInt32(value, 16) If Not isNegative And ((targetNumber And &H80000000) 0) Then Throw New OverflowException() Else Console.WriteLine("0x{0} converts to {1}.", value, targetNumber) End If Catch e As OverflowException Console.WriteLine("Unable to convert '0x{0}' to an integer.", value) End Try ' Displays the following to the console: ' Unable to convert '0x80000000' to an integer. 适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3